import {
  selectBillList,
  selectBillDetail,
  detailsInfo,
  selectBillInfo,
  selectBorrowInfo,
  selectFileList,
  billOperate,
  removeBillInfo,
  seleceBillAddInfo,
  addBillInfoDetail,
  removeBill,
  commitBill,
  // selectInfoDetail,
  enableOrDisable,
  vehicleOperate,
  seleceBillOilInfo,
  seleceBillVehicleInfo,
  detailInfoDownload,
  selectBillTax,
  recallTmsBill,
} from '@/services/fleetManage/bill';
import prompt from '@/components/Prompt';
import { caclTotal, caclAvgPrice } from '../utils';

export default {
  namespace: 'vehicle',

  state: {
    billList: {}, // 帐单列表
    billDetail: {},
    detailsInfo: {},
    billInfo: {},
    billAddInfo: {},
    formValues: {},
    oilInfoList: {},
    vehicleInfoList: {},
    borrowInfo: {},
    billTaxList: {},
  },

  effects: {
    /**
     * 查询帐单列表
     */
    *selectBillList({ payload, callback }, { call, put }) {
      const response = yield call(selectBillList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;

      yield put({
        type: 'saveAllValus',
        payload: {
          billList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    /**
     * 查询帐单详情
     */
    *selectBillDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectBillDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      const filePayLoad = { bizId: payload.id, fileBizType: 'BILL' };
      const fileResponse = yield call(selectFileList, filePayLoad);
      if (fileResponse.code == 0) {
        newData.fileList = fileResponse.data;
      }
      yield put({
        type: 'detail',
        payload: { billDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    // 明细详情
    *detailsInfo({ payload, callback }, { call, put }) {
      const response = yield call(detailsInfo, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      newData.fileList = data.attachments || [];
      yield put({
        type: 'saveAllValus',
        payload: { detailsInfo: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    /**
     * 移除账单
     */
    *removeBill({ payload, callback }, { call }) {
      const response = yield call(removeBill, payload);
      const { code, data, message } = response;
      const content = `移除账单${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    *recallTmsBill({ payload, callback }, { call }) {
      const response = yield call(recallTmsBill, payload);
      const { code, data, message } = response;
      const content = `撤回账单${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    *commitBill({ payload, callback }, { call }) {
      const response = yield call(commitBill, payload);
      const { code, data, message } = response;
      const content = `提交审核${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    /**
     * 查询帐单明细
     */
    *selectBillInfo({ payload, callback }, { call, put }) {
      const response = yield call(selectBillInfo, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      yield put({
        type: 'detail',
        payload: { billInfo: { [payload.billId]: infoList } },
      });
      callback && callback(list);
    },
    /**
     * 新增编辑帐单
     */
    *billOperate({ payload, callback }, { call }) {
      const response = yield call(billOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },

    /**
     * 移除账单明细
     */
    *removeBillInfo({ payload, callback }, { call }) {
      const response = yield call(removeBillInfo, payload);
      const { code, data, message } = response;
      const content = `移除明细${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    /**
     * 查询新增明细列表
     */
    *seleceBillAddInfo({ payload, callback }, { call, put }) {
      const response = yield call(seleceBillAddInfo, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      yield put({
        type: 'saveAllValus',
        payload: { billAddInfo: infoList },
      });
      callback && callback(list);
    },
    /**
     * 新增明细
     */
    *addBillInfoDetail({ payload, callback }, { call }) {
      const response = yield call(addBillInfoDetail, payload);
      const { code, data, message } = response;
      const content = `新增明细${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    // /**
    //  * 证件信息详情
    //  */
    // *selectInfoDetail({ payload, callback }, { call, put }) {
    //   const response = yield call(selectInfoDetail, payload);
    //   const { code, data } = response;
    //   if (code !== 0) return;
    //   if (callback) callback(data);
    // },

    /**
     * 新增编辑 证件
     */
    *infoOperate({ payload, callback }, { call, put }) {
      const response = yield call(infoOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      if (callback) callback(data);
    },
    //加油明细列表：
    *seleceBillOilInfo({ payload, callback }, { call, put }) {
      const response = yield call(seleceBillOilInfo, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      const obj = {
        price: '总计',
        oilAmount: caclTotal(data, 'oilAmount'),
        totalFee: caclTotal(data, 'totalFee'),
        currencyType: data[0].currencyType,
      };
      yield put({
        type: 'detail',
        payload: { oilInfoList: { [payload.id]: { list: [...data, obj] } } },
      });
      callback && callback([...data, obj]);
    },
    //车辆统计明细列表：
    *seleceBillVehicleInfo({ payload, callback }, { call, put }) {
      const response = yield call(seleceBillVehicleInfo, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      let obj = {
        cartPlate: '总计',
        currencyType: data[0].currencyType,
        totalFee: caclTotal(data, 'totalFee'),
        avgPrice: caclAvgPrice(data, 'avgPrice'),
        oilAmount: caclTotal(data, 'oilAmount'),
      };
      if (payload.type == 'UPKEEP') {
        obj.realTotalFee = caclTotal(data, 'realTotalFee');
      }

      yield put({
        type: 'detail',
        payload: { vehicleInfoList: { [payload.id]: { list: [...data, obj] } } },
      });
      callback && callback([...data, obj]);
    },
    *detailInfoDownload({ payload }, { call }) {
      yield call(detailInfoDownload, payload);
    },

    /**
     *  冲借款明细
     */
    *selectBorrowInfo({ payload, callback }, { call, put }) {
      const response = yield call(selectBorrowInfo, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const infoList = {
        list: data,
      };
      yield put({
        type: 'detail',
        payload: { borrowInfo: payload.id ? { [payload.id]: infoList } : infoList },
        // payload: { borrowInfo: infoList },
      });
      callback && callback(infoList);
    },
    /**
     * 查询新增明细列表 科目
     */
    *selectBillTax({ payload, callback }, { call, put }) {
      const response = yield call(selectBillTax, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      const { detailList, taxTotal, uncontainTaxTotal, containTaxTotal } = data;
      let infoList = detailList;
      infoList.push({
        id: '0',
        reasons: '',
        type: '合计',
        unContainTax: uncontainTaxTotal,
        taxRate: '',
        tax: taxTotal,
        containTax: containTaxTotal,
      });
      yield put({
        type: 'detail',
        payload: { billTaxList: { [payload.id]: { list: infoList } } },
      });
      callback && callback(list);
    },
    *billTaxValue({ payload }, { _, put }) {
      yield put({
        type: 'detail',
        payload: payload,
      });
    },
    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
